package com.zhxh.nzpos.sell;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

import com.petpet.sc.widget.SegmentedControl;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.CategoryDao;
import com.zhxh.nzpos.dao.GoodsDao;
import com.zhxh.nzpos.goods.GoodsItem;
import com.zhxh.nzpos.model.Category;
import com.zhxh.nzpos.model.Goods;
import com.zhxh.nzpos.setting.ItemActionCallback;
import com.zhxh.nzpos.setting.PriceSettingAdapter;
import com.zhxh.nzpos.setting.PriceSettingAdapter.ViewHolder;
import com.zhxh.nzpos.utils.ViewUtil;

public class GoodsSelectActivity extends FragmentActivity implements
		ItemActionCallback {

	private ArrayList<GoodsItem> goodsItems;
	private PriceSettingAdapter listAdapter;
	private SegmentedControl tabs;
	private CategoryDao categoryDao;
	public static final int PAGESIZE = 15;
	public static final String PREFS_NAME = "PreferencesFile";
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_goods_select);
		categoryDao = new CategoryDao(getApplicationContext());
		initViews();
	}


	/**
	 * 页面初始化
	 */
	private void initViews() {
		initTab();
		
		ImageView pageupBtn = (ImageView) findViewById(R.id.goods_update_btn);
		pageupBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				SharedPreferences selected = getSharedPreferences(PREFS_NAME, 0);
				String products = selected.getString("prodcuts", null);
				SharedPreferences.Editor editor = selected
						.edit();
				editor.remove("prodcuts");
				editor.commit();
				
				Intent i = new Intent();
				i.putExtra("goodsid", products);

				setResult(RESULT_OK, i);
				
				finish();
				//System.out.println("$$$$$$$$$$$$$=products=="+products);
			}
		});

	}

	
	/**
	 * 初始化一级菜单
	 */
	private void initTab() {

		tabs = (SegmentedControl) findViewById(R.id.cate);

		List<Category> categorys = categoryDao.getAllParents();
		for (int i = 0; i < categorys.size(); i++) {
			Category category = categorys.get(i);

			Button btn = new Button(this);
			btn.setTag(category.getId());
			btn.setId(i);
			btn.setOnClickListener(tabListener);
			btn.setText(category.getCategory());
			btn.setTextSize(20);
			// LayoutParameter
			// tabs.
			LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			if (i == 0) {
				btn.setBackgroundResource(R.drawable.btn_price_head);
				btn.setPressed(true);

			} else if (i == categorys.size() - 1) {
				btn.setBackgroundResource(R.drawable.btn_price_tail);
				params.leftMargin = -16;
				// params.addRule(RelativeLayout.RIGHT_OF,i-1);
			} else {
				// params.addRule(RelativeLayout.RIGHT_OF,i-1);
				btn.setBackgroundResource(R.drawable.btn_price_midle);
				params.leftMargin = -16;
			}
			// params.addRule(verb)
			tabs.addView(btn, params);
		}

	}

	// 一级菜单监听事件
	OnClickListener tabListener = new OnClickListener() {
		public void onClick(View v) {

			String cateid = (String) v.getTag();
				TitlesFragment titles = TitlesFragment.newInstance(cateid);
				FragmentTransaction ft = getSupportFragmentManager()
						.beginTransaction();
				ft.replace(R.id.categorylevel2, titles);
				ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
				ft.commit();

		}
	};

	/**
	 * 商品搜索
	 * 
	 * @param view
	 */
	public void search(View view) {
		final EditText dealOcr = (EditText) findViewById(R.id.goods_ocr_et);
		Editable ocr = dealOcr.getText();
		if (ocr != null) {
			String ocrStr = ocr.toString();
			if (goodsItems == null) {
				goodsItems = new ArrayList<GoodsItem>();
			} else {
				goodsItems.clear();

			}
			GoodsDao dao = new GoodsDao(this);
			Goods goods = dao.find(ocrStr.trim());
			if (goods != null) {
				GoodsItem item = copyProperties(goods);
				goodsItems.add(item);
				if (listAdapter != null) {
					listAdapter.notifyDataSetChanged();
				}
			} else {
				ViewUtil.makeToast(this, "没有找到相关商品");
			}
		}
	}

	private GoodsItem copyProperties(Goods goods) {
		GoodsItem item = new GoodsItem();
		item.setId(goods.getId());
		item.setCategoryId(goods.getCategoryId());
		item.setCategoryName(goods.getCategoryName());
		item.setName(goods.getName());
		item.setNote(goods.getNote());
		item.setPrice1(goods.getPrice1());
		item.setPrice2(goods.getPrice2());
		item.setSpecification(goods.getSpecification());
		item.setUnits1(goods.getUnits1());
		item.setUnits2(goods.getUnits2());
		item.setPoints(goods.getPonits());
		item.setCode(goods.getCode());
		return item;
	}

	@Override
	public void onUpdate(int position, ViewHolder holder) {
		// TODO Auto-generated method stub

	}
	
	@Override
	protected void onStop() {
		super.onStop();
		SharedPreferences selected = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = selected
				.edit();
		editor.remove("prodcuts");
		editor.commit();
	}

}
